package common;

import java.io.Serializable;
import java.util.LinkedList;

//This class steps through the possible ways your character can walk after throwing the dice.
//It creates a new instance of it self every time it finds another way.
//Then every class returns the tile it stands at when it has stepped through the rolled number.
public class Step implements Serializable {
	private static final long serialVersionUID = 1L;
	private final PlayerGameBoard gameboard;
	private final int movement;
	private final Tile currentTile;
	private final Tile lastTile;
	private final LinkedList<Tile> tileList;

	public Step(final PlayerGameBoard sgb, final int n, final Tile currentTile,
			final LinkedList<Tile> tileList) {
		this.gameboard = sgb;
		this.movement = n;
		this.currentTile = currentTile;
		this.lastTile = currentTile;
		this.tileList = tileList;
		step();

	}

	public Step(final PlayerGameBoard sgb, final int n, final Tile currentTile,
			final Tile lastTile, final LinkedList<Tile> tileList) {
		this.gameboard = sgb;
		this.movement = n;
		this.currentTile = currentTile;
		this.lastTile = lastTile;
		this.tileList = tileList;
		step();
	}

	private void step() {
		if (this.movement != 0) {
			for (int i = 0; i < this.currentTile.getNeighbours().size(); i++) {
				if (this.tileList.get(this.currentTile.getNeighbours().get(i))
						.position() != this.lastTile.position()) {
					new Step(this.gameboard, this.movement - 1,
							this.tileList.get(this.currentTile.getNeighbours()
									.get(i)), this.currentTile, this.tileList);
				}
			}
		} else {
			this.gameboard.addAvailableTile(this.currentTile);
		}
	}
}
